/* MarkusCrawler
 *
 * Copyright (C) 2011 by Markus '[mackan]' Hyttinen <mackanhyttinen@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


package me.crawler;


import java.sql.*;


public class CrawlerDBManager {
	
	
	protected String dbUrl , userName, password;
	protected int flushInterval;
	/**
	 * @return the flushInterval
	 */
	public synchronized int getFlushInterval() {
		return flushInterval;
	}





	/**
	 * @param flushInterval the flushInterval to set
	 */
	public synchronized void setFlushInterval(int flushInterval) {
		this.flushInterval = flushInterval;
	}

	private final String CREATE_DATABASE = "CREATE DATABASE IF NOT EXISTS CRAWLER";
	private final String CREATE_HTML_TABLE = "CREATE TABLE IF NOT EXISTS  CRAWLER.HTML_PAGE (" +
			"ID int(11) NOT NULL AUTO_INCREMENT," +
			"PAGE_ID int(11) DEFAULT NULL," +
			"URL varchar(1255) DEFAULT NULL," +
			"DATE_CRAWLED datetime DEFAULT NULL," +
			"PARENT int(11) DEFAULT NULL," +
			"DEPTH varchar(45) DEFAULT NULL," +
			"CONTENT longblob,"+
			"PRIMARY KEY (ID)," +
			"KEY ID_INDEX (ID)," +
			"KEY URL_INDEX (URL));";
	
	
	private boolean threadRunning = false;



	public CrawlerDBManager(String _dbUrl , String _userName , String _password ){
		dbUrl = _dbUrl;
		userName = _userName;
		password = _password;
		flushInterval = 300;
		createDB();
	}
	
	
	
	

	public void createDB(){

		Connection conn = null;
		Statement stmt = null;
		try{

			Class.forName("com.mysql.jdbc.Driver");

			System.out.println("CONNECTING TO DATABASE SERVER");
			conn = DriverManager.getConnection(dbUrl, userName, password);
			System.out.println("CONNECTED TO SERVER");

			System.out.println("CREATING DATABASE IF NOT ALLREADY EXIST");
			stmt = conn.createStatement();
			stmt.executeUpdate(CREATE_DATABASE);


			System.out.println("CREATING HTML TABLE IF NOT ALLREADY EXIST");
			stmt.executeUpdate(CREATE_HTML_TABLE);




		}catch(SQLException se){
			se.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{

			try{
				if(stmt!=null)
					stmt.close();
			}catch(SQLException se2){
				se2.printStackTrace();
			}
			try{
				if(conn!=null)
					conn.close();
			}catch(SQLException se){
				se.printStackTrace();
			}
		}
	}

	
	public  void flushToDB(CrawlerQueue crawlerQueue){
		
		while(threadRunning){
		}
		if (!threadRunning){
			threadRunning = true;
			CrawlerHTMLDumpThread flusherThread = new CrawlerHTMLDumpThread(crawlerQueue , dbUrl , userName , password);		
			flusherThread.setController(this);
			flusherThread.start();
			
		}
	}

	public void threadFinished(){
		threadRunning = false;	
	}


	public String getDbUrl() {
		return dbUrl;
	}

	public void setDbUrl(String dbUrl) {
		this.dbUrl = dbUrl;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}
